package ic.types;

import common.Utils;
import ic.ast.decl.DeclMethod;
import ic.ast.decl.Parameter;
import ic.ast.decl.Type;

import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Matan
 * Date: 12/4/13
 */
public class MethodType {

    private List<Type> paramTypes = new LinkedList<Type>();
    private Type returnType;

    public MethodType(DeclMethod method) {
        returnType = method.getType();

        for (Parameter formal : method.getFormals()) {
            paramTypes.add(formal.getType());
        }
    }

    public Type getReturnType() {
        return returnType;
    }

    public List<Type> getParamTypes() {
        return paramTypes;
    }

    public String toString() {
        StringBuilder str = new StringBuilder();
        str.append(Utils.prettyPrintList(paramTypes));
        str.append(" -> ");
        str.append(returnType);
        return str.toString();
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof MethodType))
            return false;

        MethodType m = (MethodType) o;
        return (this.getParamTypes().toString().equals(m.getParamTypes())) &&
                this.getReturnType().toString().equals(m.getReturnType());
    }

}
